package com.linkai.controller.admin.info;


import com.linkai.controller.admin.BaseController;
import com.linkai.entity.Supplier;
import com.linkai.enums.ResultDTO;
import com.linkai.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yamon
 * @since 2021-02-27
 */
@Controller
@RequestMapping("/admin/info/supplier")
public class SupplierController extends BaseController implements InfoBaseController{


    @Autowired
    private SupplierService supplierService;
    /**
     * 首页
     * @return index
     */
    @GetMapping("/index")
    public ModelAndView index(){
        final ModelAndView modelAndView = new ModelAndView();
        final List<Supplier> list = supplierService.list();
        modelAndView.addObject("suppliers",list);
        modelAndView.setViewName("admin/info/supplier/index");
        return modelAndView;
    }

    /**
     * 跳转增加页面
     * @return list
     */
    @GetMapping("/add")
    public String add(){
        return "admin/info/supplier/add";
    }

    /**
     * 跳转到页面，修改页面并获取数据
     * @param id id
     * @return page
     */
    @GetMapping("/add/{id}")
    public ModelAndView add(@PathVariable Integer id){
        final ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("store",supplierService.getById(id));
        modelAndView.setViewName("admin/info/supplier/add");
        return modelAndView;
    }
    /**
     * 保存或者修改一个职员信息
     * @return BOOLEAN
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public ResultDTO<Boolean> addEmployee(@RequestBody Supplier supplier){
        if (supplier.getId()!=null){
            //更新
            return super.updateInfo(supplierService.updateById(supplier));
        }
        return super.successSaveInfo(supplierService.save(supplier));
    }

    /**
     * 根据id得到实体类
     * @param id id
     * @return 实体类
     */
    @GetMapping("/get/{id}")
    public Supplier getOneEmployee(@PathVariable Integer id){
        return supplierService.getById(id);
    }



    /**
     * 根据id删除所有职员信息
     * @param id id
     * @return flag
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResultDTO<Boolean> deleteOneEmployee(@PathVariable Integer id){
        return super.successDeleteInfo(supplierService.removeById(id));
    }


    @Override
    public ResultDTO<Boolean> saveSize(String sizeDesc) {
        return null;
    }

    @Override
    @GetMapping("/list")
    @ResponseBody
    public List<Supplier> listAll() {
        return supplierService.list();
    }
}

